package com.poker.service;

import com.poker.entity.Activity;
import com.poker.entity.ActivityOuts;
import com.poker.repository.ActivityOutsRepository;
import com.poker.repository.ActivityRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.Date;

/**
 * @ClassName ActivityOutsService
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 13:44
 * @Version 1.0
 **/
@Service
public class ActivityOutsService {

    @Resource
    private ActivityOutsRepository activityOutsRepository;

    @Resource
    private ActivityRepository activityRepository;

    public Page<ActivityOuts> findList(Integer page, Integer size,Integer classifya) {
        PageRequest pb =PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        if(classifya!=null){
            Specification<ActivityOuts> spec = new Specification<ActivityOuts>() {        //查询条件构造
                public Predicate toPredicate(Root<ActivityOuts> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                    Path<Integer> classify = root.get("classify");
                    Predicate p1 = cb.equal(classify, classifya);
                    Predicate p = cb.and(p1);
                    return p;
                }
            };
            return activityOutsRepository.findAll(spec,pb);
        }

        return activityOutsRepository.findAll(pb);
    }

    public Page<ActivityOuts> findListAll(Integer page, Integer size, ActivityOuts activityOuts, Integer classifya) {
        Pageable pageable = PageRequest.of(page, size,Sort.Direction.DESC, "createTime");
        Specification<ActivityOuts> spec = new Specification<ActivityOuts>() {        //查询条件构造
            public Predicate toPredicate(Root<ActivityOuts> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> outsTitle = root.get("outsTitle");
                Path<String> activityName = root.get("activityName");
                Path<Integer> classify = root.get("classify");
                Predicate p0 = cb.equal(classify, classifya);
                Predicate p1 = cb.like(outsTitle, "%"+activityOuts.getOutsTitle()+"%");
                Predicate p2 = cb.like(activityName,"%"+activityOuts.getActivityName()+"%");
                Predicate p = cb.and(p0,p1,p2);
                return p;
            }
        };
        return  activityOutsRepository.findAll(spec,pageable);
    }

    public ActivityOuts getById(String id) {
        return activityOutsRepository.getOne(id);
    }

    public void update(ActivityOuts activityOuts) {
        activityOuts.setActivityName(getActivityName(activityOuts.getActivityId()));
        activityOuts.setClassify(getActivityClassify(activityOuts.getActivityId()));
        activityOuts.setInputTime(new Date());
        ActivityOuts one = activityOutsRepository.getOne(activityOuts.getId());
        activityOuts.setInputTime(one.getInputTime());
        activityOutsRepository.saveAndFlush(activityOuts);

    }

    public void saveActivityOuts(ActivityOuts activityOuts) {
        activityOuts.setActivityName(getActivityName(activityOuts.getActivityId()));
        activityOuts.setClassify(getActivityClassify(activityOuts.getActivityId()));
        activityOuts.setInputTime(new Date());
         activityOutsRepository.saveAndFlush(activityOuts);
    }

    public void deleteActivityOuts(ActivityOuts outs) {
        activityOutsRepository.delete(outs);
    }

    public String getActivityName(String activityId){
        Activity one = activityRepository.getOne(activityId);
        return one.getActivityName();
    }
    public Integer getActivityClassify(String activityId){
        Activity one = activityRepository.getOne(activityId);
        return one.getClassify();
    }
}
